package cn.elves.model.auth.controller;

import cn.elves.model.auth.entity.dto.ResponseDTO;
import cn.elves.model.auth.entity.dto.UserDTO;
import cn.elves.model.auth.entity.po.User;
import cn.elves.model.auth.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Set;

@RestController
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("user/page/{pageNum}/{pageSize}")
    public ResponseDTO<Page<User>> getUserPage(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {

        return ResponseDTO.success(userService.page(new Page<>(pageNum, pageSize)));
    }

    @GetMapping("user/{id}")
    public ResponseDTO<User> getUserDetail(@PathVariable("id") Long id) {
        return ResponseDTO.success(userService.getById(id));
    }

    @PostMapping("user")
    public ResponseDTO<User> saveUser(@RequestBody UserDTO UserDTO) {
        userService.save(UserDTO.toUser());
        return ResponseDTO.success();
    }

    @PutMapping("user")
    public ResponseDTO<User> updateUser(@RequestBody UserDTO UserDTO) {
        userService.updateById(UserDTO.toUser());
        return ResponseDTO.success();
    }

    @DeleteMapping("user/{id}")
    public ResponseDTO<User> deleteUserById(@PathVariable("id") Long id) {
        userService.removeById(id);
        return ResponseDTO.success();
    }

    @PatchMapping("allocationRole/{id}")
    public ResponseDTO<User> allocationRole(@PathVariable("id") Long userId, @RequestBody Set<Long> roleIdSet) {
        userService.allocationRole(userId,roleIdSet);
        return ResponseDTO.success();
    }

}
